//
//  CustomScrollView.m
//  循环滚动视图
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "CustomScrollView.h"

@interface CustomScrollView ()<UIScrollViewDelegate>

/** 滚动视图属性 */
@property (nonatomic, retain) UIScrollView *customScrollView;
/** 指示器属性 */
@property (nonatomic, retain) UIPageControl *pageControll;

//@property (nonatomic, retain) UIScrollView *smallScrollView;
@end


@implementation CustomScrollView

- (void)dealloc
{
    [_customScrollView release];
    [_pageControll release];
    [_imageArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self createSubViews];
        [self setpropertys];

        //初始化视图时先把存载数据的空间开辟
//        self.imageArray = [NSArray array];
        

    }
    return self;
}

//创建滚动视图和指示器
- (void)createSubViews
{
    self.customScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.customScrollView.delegate = self;
    [self addSubview:_customScrollView];
    [_customScrollView release];
    
    
    self.pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) / 5.0 * 4.0, CGRectGetWidth(self.customScrollView.bounds), CGRectGetHeight(self.bounds) / 5.0 * 0.5)];
    
    [self addSubview:_pageControll];
    [_pageControll release];
}



//设置滚动视图属性
- (void)setpropertys
{
    self.customScrollView.backgroundColor = [UIColor cyanColor];
    
    //设置视图偏移量
    self.customScrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
    self.customScrollView.pagingEnabled = YES;
    self.customScrollView.showsVerticalScrollIndicator = NO;
    self.customScrollView.bounces = NO;
    
    
    
    
    self.pageControll.pageIndicatorTintColor = [UIColor orangeColor];
    self.pageControll.currentPageIndicatorTintColor = [UIColor blueColor];
    
}


- (void)setImageArray:(NSArray *)imageArray
{
    if (_imageArray != imageArray) {
        [_imageArray release];
        _imageArray = [imageArray retain];
    }
    //数组有值得时候 调用下面的方法
    [self addPictures:imageArray];
    
    //设置pageControl 指示器下标的个数
    self.pageControll.numberOfPages = imageArray.count;
    
    
}

//picArray 只有重写setter方法, 数据才能传进来<setter方法中调用下面这个方法>
- (void)addPictures:(NSArray<UIImage *> *)picArray
{
    //设置滚动区域 循环滚动需要根据数据数组的个数加 2
    self.customScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) * (picArray.count + 2), CGRectGetHeight(self.bounds));
    
    for (NSInteger i = 0; i < picArray.count; i++) {
        //创建小滚动视图
        UIScrollView *smallScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) * (i + 1), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
        smallScroll.contentSize = CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        smallScroll.delegate = self;
        smallScroll.minimumZoomScale = 0.5;//缩放最小限度
        smallScroll.maximumZoomScale = 2.0;//缩放最大限度
        smallScroll.showsHorizontalScrollIndicator = NO;
        [self.customScrollView addSubview:smallScroll];
        [smallScroll release];

        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:picArray[i]];
        imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        [smallScroll addSubview:imageView];
        [imageView release];
    }
    
//    设置第一张图
    UIScrollView *frontScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    frontScroll.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
    [self.customScrollView addSubview:frontScroll];
    [frontScroll release];
    
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    firstImageView.image = [picArray lastObject];
    [frontScroll addSubview:firstImageView];
    [firstImageView release];

//    设置最后一张图片
    UIScrollView *behindScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) * (picArray.count + 1), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
    behindScroll.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
    [self.customScrollView addSubview:behindScroll];
    [behindScroll release];
    
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    lastImageView.image = [picArray firstObject];
    [behindScroll addSubview:lastImageView];
    [lastImageView release];
    
}

//缩放的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    for (UIScrollView *scroll in scrollView.subviews)
    {
        
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            [UIView animateWithDuration:0.5f animations:^{
                scroll.zoomScale = 1.0;
            }];
            
        }
    }

    //指示器当前的下标
    self.pageControll.currentPage = self.customScrollView.contentOffset.x / self.customScrollView.bounds.size.width - 1;
    
    //根据当前偏移量判断
    //左划
    if (scrollView.contentOffset.x == 0) {
        //修改偏移量:
        self.customScrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds) * (self.imageArray.count), 0);
        self.pageControll.currentPage = self.imageArray.count;
    }
    //右划
    if (scrollView.contentOffset.x >= CGRectGetWidth(self.bounds) * (self.imageArray.count + 1)) {
        self.customScrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
        self.pageControll.currentPage = 0;
    }
}

//调用这个方法会改变滚动视图的偏移量 
- (void)changeScrollViewContentOffSet:(NSInteger)number
{
    self.customScrollView.contentOffset = CGPointMake(number * CGRectGetWidth(self.bounds), 0);
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    return scrollView.subviews.firstObject;
}




@end







